<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:26
 */
namespace App;
defined('IN_IA') or exit('Access Denied');
use Model\MemberMod;
use Model\WqFansMod;
use Model\WqMemberMod;
use Pub\cache\Cache;
use Pub\tool\Setting as SettingCache;

class Action
{
    protected $_data = array();
    protected $_result = array('status'=>'error');
    protected $_gpc = array();
    protected $_w = array();
    protected $_uniacid = 0; //公众号id
    protected $_member_uid = 0;  //用户id


    private $WqMember = null;  //微擎会员
    private $Member = null;
    private $WqFans = null; //微擎粉丝

    protected function __construct()
    {
        global $_GPC,$_W;
        $this->_gpc = $_GPC;
        $this->_w = $_W;
        $this->_member_uid = $_W["member"]["uid"];
        $this->_uniacid = $_W["account"]["uniacid"];
    }

    /**
     * 接口返回内容处理
     * @param $_data
     */
    protected function result($_data)
    {
        if (is_array($_data)) {
            echo json_encode($_data);
        } else {
            echo $_data;
        }
        exit;
    }

    /**
     * 操作积分余额
     * @param number $_uid 用户id
     * @param number $_balance 操作的余额积分
     * @param string $_type add 或者 sub
     * @return boolean 是否操作成功
     */
    protected function handleBalance($_uid,$_balance,$_type = "add")
    {
        $_use_balance = SettingCache::get(SettingCache::USE_BALANCE);
        if($_use_balance == 1){
            $this->Member = new MemberMod();
            if($_type == "add"){
                return $this->Member->math(array("balance +"=>$_balance),array("uniacid"=>$this->_uniacid,"uid"=>$_uid));
            }else{
                $_member = $this->Member->findOne(array("uniacid"=>$this->_uniacid,"uid"=>$_uid),array("balance"));
                if($_member > 0){
                    return $this->Member->math(array("balance -"=>$_balance),array("uniacid"=>$this->_uniacid,"uid"=>$_uid));
                }else{
                    return true;
                }
            }
        }else{
            if($_type == "add"){
                return mc_credit_update($_uid,"credit1",$_balance);
            }else{
                $this->WqMember = new WqMemberMod();
                $_wq_member = $this->WqMember->findOne(array("uid"=>$_uid),array("credit1"));
                if($_wq_member["credit1"] > 0){
                    return mc_credit_update($_uid,"credit1",-$_balance);
                }else{
                    return true;
                }
            }
        }
    }

    /**
     * 获取指定用户的余额
     * @param $_uid
     * @return double 余额
     */
    protected function getBalance($_uid)
    {
        $this->Member = new MemberMod();
        $_member = $this->Member->findOne(array("uid"=>$_uid),array("balance"));
        $_use_balance = SettingCache::get(SettingCache::USE_BALANCE);
        $_balance = $_member["balance"];
        if($_use_balance == 2){
            $_balance = $this->getWqBalance($_uid);
        }
        return $_balance;
    }

    /**
     * 获取微擎余额
     * @param $_uid 用户id
     * @return double 用户余额
     */
    private function getWqBalance($_uid)
    {
        $this->WqMember = new WqMemberMod();
        $_member = $this->WqMember->findOne(array('uid'=>$_uid),array("credit1"));
        return $_member["credit1"];
    }


    /**
     * 通过uid获取用户信息,如果微擎表里没有获取到用户的昵称和头像则手动获取设置微擎表的用户头像和昵称
     * @param $_uid
     * @return mixed
     */
    protected function uidToInfo($_uid)
    {
        $_member = Cache::get(Cache::MEMBER,array("uid"=>$_uid));
        if(!$_member || KUYUAN_DEBUG){
            $_member = $this->getMemberInfo($_uid);
            Cache::set(Cache::MEMBER,$_member,array("uid"=>$_uid));
        }
        return $_member;
    }

    /**
     * 通过uid获取用户信息,如果微擎表里没有获取到用户的昵称和头像则手动获取设置微擎表的用户头像和昵称
     * @param $_uid
     * @param array $_return
     * @param number $_count 循环次数
     * @return mixed
     */
    private function getMemberInfo($_uid,Array $_return = array('nickname','avatar'),$_count = 0)
    {
        if(!$this->WqMember instanceof WqMemberMod) $this->WqMember = new WqMemberMod();
        $_member = $this->WqMember->findOne(array('uid'=>$_uid),$_return);
        if($_count ==0){
            if((isset($_member['nickname']) && $_member['nickname'] =='') || (isset($_member['avatar']) && $_member['avatar'] == '')){
                $WxApi = \WeAccount::create();
                $_fans = $this->uidToFans($_uid);
                $_wx_member = $WxApi->fansQueryInfo($_fans['openid']);
                $this->WqMember->update(array('nickname'=>$_wx_member['nickname'],'avatar'=>$_wx_member['headimgurl']),array('uid'=>$_uid));
                return $this->getMemberInfo($_uid,$_return,++$_count);
            }
        }
        return $_member;
    }

    /**
     * 通过uid获取用户的粉丝信息
     * @param $_uid
     * @param array $_return
     * @return
     */
    protected function uidToFans($_uid,Array $_return = array('openid'))
    {
        if(!$this->WqFans instanceof WqFansMod) $this->WqFans = new WqFansMod();
        return $this->WqFans->findOne(array('uid'=>$_uid),$_return);
    }

    //---------二维码-------------

    /**
     * 获取，生成一个二维码
     * @param string $_dir_name 二维码存放的目录名字
     * @param string $_file_name 文件名
     * @param string $_url  二维码储存的链接
     * @param boolean $_build 是否重新生成
     * @return mixed
     */
    protected function getQrcode($_dir_name,$_file_name,$_url,$_build = false)
    {
        $_dir = ATTACHMENT_ROOT.'kuyuan_red/'.$this->_uniacid.'/'.$_dir_name.'/';
        $_filename = $_dir.$_file_name.'.png';
        $_path = 'kuyuan_red/'.$this->_uniacid.'/'.$_dir_name.'/'.$_file_name.'.png';
        if(is_file($_filename) && !$_build){
            return $this->_w['attachurl_local'].$_path;
        }else{
            if(!is_dir($_dir)){
                mkdir($_dir,0777,true);
            }
            $Qrcode = new QrCode($_url);
            $Qrcode->setSize(500);
            $Qrcode->save($_filename);
            return $this->_w['attachurl_local'].$_path;
        }
    }
}